package com.yanqu.road.server.cmd.palace;

import com.yanqu.road.entity.estatewar.palace.PalaceBattleNotice;
import com.yanqu.road.logic.pb.PalacePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.palace.CrossPalaceActivity;
import com.yanqu.road.server.manager.palace.CrossPalaceActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Protocol.C_PALACE_BATTLE_NOTICE, desc = "获取公告")
public class CrossPalaceBattleNoticeListCmd extends CrossCommand {
    private long userId;
    private int activityId;
    private List<PalaceBattleNotice> dataList;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceBattleNoticeReqMsg reqMsg = EstateWarProto.PalaceBattleNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossPalaceActivity crossPalaceActivity = CrossPalaceActivityMgr.getCrossPalaceActivity(activityId);
        if(null == crossPalaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        dataList = crossPalaceActivity.getPalaceBattleNoticeList();
    }

    @Override
    public void sendData() {
        EstateWarProto.PalaceBattleNoticeRespMsg.Builder respMsg = EstateWarProto.PalaceBattleNoticeRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret) {
            respMsg.setDataList(PalacePb.parsePalaceBattleNoticeListMsg(dataList));
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_PALACE_BATTLE_NOTICE_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }
}
